/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.UserBean;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import radio.entities.Accounts;
import radio.entities.PaymentDetails;
import radio.sessions.AccountsFacade;
import radio.sessions.PaymentDetailsFacade;

/**
 *
 * @author Huu Vinh
 */
public final class Admin {

    @EJB
    private PaymentDetailsFacade paymentDetailsFacade;
    @EJB
    private AccountsFacade accountsFacade;
    private UserBean user;
    private UserBean user1;
    private PaymentDetails pd;
    private String rePass;
    private String oldPass;
    private String newPass;

    public Admin() {
    }

    public UserBean getUser1() {
        return user1;
    }

    public void setUser1(UserBean user1) {
        this.user1 = user1;
    }

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }

    public PaymentDetails getPd() {
        return pd;
    }

    public void setPd(PaymentDetails pd) {
        this.pd = pd;
    }

    public void setRePass(String rePass) {
        this.rePass = rePass;
    }

    public void setOldPass(String oldPass) {
        this.oldPass = oldPass;
    }

    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }

    public String getNewPass() {
        return newPass;
    }

    public String getOldPass() {
        return oldPass;
    }

    public String getRePass() {
        return rePass;
    }

    public void getAccount() {
        Accounts temp = new Accounts();
        temp = accountsFacade.findbyId(0);
        if (temp != null) {
            user = new UserBean(temp.getUid(), temp.getPwd(), temp.getImg(), temp.getAccountDetails().getContactPerson(), temp.getAccountDetails().getMobile(), temp.getAccountDetails().getTelephone(), temp.getAccountDetails().getAddress(), temp.getAccountDetails().getEmail(), temp.isGender(), temp.getStatus(), temp.getAccId(), temp.getTypeId().getTypeId(), temp.getAccountDetails().getCityId().getCityId());
        } else {
            System.out.println("dkm null");
        }
    }

    public String show() {
        getAccount();
        return "infor.show();";
    }

    public List<PaymentDetails> showInfor() {
        return paymentDetailsFacade.getIdByStatus(false);
    }

    public void onRowSelect(SelectEvent event) {
        Accounts temp = accountsFacade.findbyId(((Accounts) event.getObject()).getAccId());
        user1.setUid(temp.getUid());
        user1.setContact(temp.getAccountDetails().getContactPerson());
        user1.setTele(temp.getAccountDetails().getTelephone());
        user1.setMobile(temp.getAccountDetails().getMobile());
        user1.setEmail(temp.getAccountDetails().getEmail());
    }

    public void changePassword() {
        FacesMessage msg = null;
        getAccount();
        boolean changed = false;
        System.out.println("1" + user.getPwd());
        System.out.println("2" + oldPass);
        if (!user.getPwd().equals(oldPass)) {
            msg = new FacesMessage("Old Password is wrong");
            changed = false;
        } else if (!newPass.matches("^[A-Za-z0-9_-]{4,15}$")) {
            msg = new FacesMessage("New Password is invalid");
            changed = false;
        } else if (!rePass.matches("^[A-Za-z0-9_-]{4,15}$")) {
            msg = new FacesMessage("New Password is invalid");
            changed = false;
        } else if (!newPass.equals(rePass)) {
            msg = new FacesMessage("Confirm Password is invalid");
            changed = false;
        } else {
            try {
                Accounts temp = accountsFacade.findbyId(0);
                temp.setPwd(newPass);
                accountsFacade.edit(temp);
                msg = new FacesMessage("Change Password is successfull!!!");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                changed = true;
            } catch (Exception ex) {
                msg = new FacesMessage("Error");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                ex.printStackTrace();
                changed = false;
            }
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        RequestContext.getCurrentInstance().addCallbackParam("loggedIn", changed);
    }
}
